package com.bh.web.controller.extend;

import com.bh.common.annotation.Log;
import com.bh.common.core.controller.BaseController;
import com.bh.common.core.domain.AjaxResult;
import com.bh.common.core.page.TableDataInfo;
import com.bh.common.enums.BusinessType;
import com.bh.common.utils.poi.ExcelUtil;
import com.bh.extend.domain.JcCustomer;
import com.bh.extend.service.IJcCustomerService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 客户管理Controller
 *
 * @author slx
 * @date 2023-06-15
 */
@RestController
@RequestMapping("/extend/customer")
public class JcCustomerController extends BaseController {
    @Autowired
    private IJcCustomerService jcCustomerService;

    /**
     * 查询客户管理列表
     */
    @ApiOperation("查询客户管理列表")
    @PreAuthorize("@ss.hasPermi('extend:customer:list')")
    @GetMapping("/list")
    public TableDataInfo list(JcCustomer jcCustomer) {
        startPage();
        List<JcCustomer> list = jcCustomerService.selectJcCustomerList(jcCustomer);
        return getDataTable(list);
    }


    /**
     * 查询客户管理列表
     */
    @ApiOperation("查询客户管理列表")
    @GetMapping("/search/{keyword}")
    public AjaxResult search(@PathVariable String keyword) {
        startPage();
        List<JcCustomer> list = jcCustomerService.searchJcCustomerList(keyword);
        return AjaxResult.success(list);
    }

    /**
     * 导出客户管理列表
     */
    @PreAuthorize("@ss.hasPermi('extend:customer:export')")
    @Log(title = "客户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JcCustomer jcCustomer) {
        List<JcCustomer> list = jcCustomerService.selectJcCustomerList(jcCustomer);
        ExcelUtil<JcCustomer> util = new ExcelUtil<JcCustomer>(JcCustomer.class);
        util.exportExcel(response, list, "客户管理数据");
    }

    /**
     * 获取客户管理详细信息
     */
    @ApiOperation("获取客户管理详细信息")
    @PreAuthorize("@ss.hasPermi('extend:customer:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(jcCustomerService.selectJcCustomerById(id));
    }

    /**
     * 新增客户管理
     */
    @ApiOperation("新增客户管理")
    @PreAuthorize("@ss.hasPermi('extend:customer:add')")
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JcCustomer jcCustomer) {
        JcCustomer old = jcCustomerService.selectJcCustomerByCode(jcCustomer.getCode());
        if (old != null) {
            return error("合同号不能重复！");
        }
        return toAjax(jcCustomerService.insertJcCustomer(jcCustomer));
    }

    /**
     * 修改客户管理
     */
    @ApiOperation("修改客户管理")
    @PreAuthorize("@ss.hasPermi('extend:customer:edit')")
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JcCustomer jcCustomer) {
        JcCustomer old = jcCustomerService.selectJcCustomerByCode(jcCustomer.getCode());
        if (old != null && !old.getId().equals(jcCustomer.getId())) {
            return error("合同号不能重复！");
        }
        return toAjax(jcCustomerService.updateJcCustomer(jcCustomer));
    }

    /**
     * 删除客户管理
     */
    @ApiOperation("删除客户管理")
    @PreAuthorize("@ss.hasPermi('extend:customer:remove')")
    @Log(title = "客户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(jcCustomerService.deleteJcCustomerByIds(ids));
    }
}
